package com.javashow.main.info.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javashow.main.info.dao.IMainInfoDao;
import com.javashow.main.info.service.IMainInfoService;
import com.javashow.pojo.GridColumn;

@Service(value = "mainInfoService")
public class MainInfoServiceImpl implements IMainInfoService{
	@Autowired
	private IMainInfoDao mainInfoDao;

	@SuppressWarnings("unchecked")
	@Override
	public JSONArray getColumnsByCode(String code) {
		JSONArray result = new JSONArray();

		List<GridColumn> gridColumns = mainInfoDao.queryColumnsByCode(code);
		if(gridColumns != null && gridColumns.size() > 0) {
			for(GridColumn item : gridColumns) {
				result.add(getSingleObject(item));
			}
		}
		
		return result;
	}
	
	private JSONObject getSingleObject(GridColumn gc) {
		JSONObject result = new JSONObject();
		
		if(gc.getName() != null) {
			result.accumulate("text", gc.getName());
		}
		
		if(gc.getDataIndex() != null) {
			result.accumulate("dataIndex", gc.getDataIndex());
		}
		
		if(gc.getWidth() != null) {
			result.accumulate("width", gc.getWidth());
		}
		
		if(gc.getField() != null) {
			result.accumulate("field", gc.getField());
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject getDeptInfo() {
		JSONObject result = new JSONObject();
		JSONArray array = new JSONArray();
		
		List list = mainInfoDao.queryDeptInfo();
		if(list != null && list.size() > 0) {
			for(int i=0; i<list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				JSONObject tmp = new JSONObject();
				tmp.accumulate("code", obj[1]);
				tmp.accumulate("name", obj[3]);
				tmp.accumulate("manager", obj[2]);
				tmp.accumulate("description", obj[4]);
				
				array.add(tmp);
			}
		}
		
		result.accumulate("totalProperty", list.size());
		result.accumulate("rows", array);
		
		return result;
	}

	@Override
	public String getStoreByCode(String code) {
		String result = null;
		List list = new ArrayList();
		
		list = mainInfoDao.queryStoreNameByCode(code);
		
		if(list != null && list.size() > 0) {
			Object[] obj = (Object[]) list.get(0);
			result = obj[2].toString();
		}
		
		return result;
	}
}


















